package todo;


import se.lth.cs.realtime.*;
import done.AbstractWashingMachine;


public class SpinController extends PeriodicThread {
	AbstractWashingMachine washingMachine;
	int currentMode;
	RTThread source;
	long time;
	double speed;
	
	// TODO: add suitable attributes

	public SpinController(AbstractWashingMachine mach, double speed) {
		super((long) (1000/speed)); // TODO: replace with suitable period
		washingMachine = mach;
		time = 0;
		this.speed = speed;
		
	}

	public void perform() {
		// TODO: implement this method
		time += 1000;
		
		boolean left = time/60000 %2 == 1;
		
		SpinEvent spinEvent = (SpinEvent) mailbox.tryFetch();
		
		if (spinEvent != null){
			currentMode = spinEvent.getMode();
		}
		
		switch (currentMode){
		case SpinEvent.SPIN_OFF:
			washingMachine.setSpin(AbstractWashingMachine.SPIN_OFF);
			break;
			
		case SpinEvent.SPIN_SLOW:
			if (left){
				washingMachine.setSpin(AbstractWashingMachine.SPIN_LEFT);
			} else {
				washingMachine.setSpin(AbstractWashingMachine.SPIN_RIGHT);
			}
			break;
			
		case SpinEvent.SPIN_FAST:
			if (washingMachine.getWaterLevel() > 0.1){
				System.out.println("You are not allowed to spin fast when there is water in the machine");
				washingMachine.setSpin(AbstractWashingMachine.SPIN_OFF);
			} else {
				washingMachine.setSpin(AbstractWashingMachine.SPIN_FAST);
			}
			
			break;
		}
	}
}
